package bsts.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Customer implements Serializable, Comparable<Customer> {
	
	private static final long serialVersionUID = 1L;
	private String surname;
	private String firstname;
	private String secondname;
	private String nric;
	private String address;
	private String telephoneNo;
	private String salutation;
	private ArrayList<Interest> interests;
	private HashMap<String, Account> accounts;
	
	public Customer(String surname, String firstname, String secondname, String nric, String address, String telephoneNo, String salutation) {
		this.surname = surname;
		this.firstname = firstname;
		this.secondname = secondname;
		this.nric = nric.toUpperCase();
		this.address = address;
		this.telephoneNo = telephoneNo;
		this.salutation = salutation;
		this.interests = new ArrayList<Interest>();
		this.accounts = new HashMap<String, Account>();
	}
	
	public Customer(String surname, String firstname, String nric, String address, String telephoneNo, String salutation) {
		this.surname = surname;
		this.firstname = firstname;
		this.secondname = null;
		this.nric = nric.toUpperCase();
		this.address = address;
		this.telephoneNo = telephoneNo;
		this.salutation = salutation;
		this.interests = new ArrayList<Interest>();
		this.accounts = new HashMap<String, Account>();
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getSecondname() {
		return secondname;
	}

	public void setSecondname(String secondname) {
		this.secondname = secondname;
	}
	
	public String getFullname() {
		if ( getSecondname() == null || getSecondname().length() <= 0 ) {
			return getSurname() + " " + getFirstname();
		} else {
			return getSurname() + " " + getFirstname() + " " + getSecondname();
		}
	}

	public String getNric() {
		return nric;
	}

	public void setNric(String nric) {
		this.nric = nric.toUpperCase();
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTelephoneNo() {
		return telephoneNo;
	}

	public void setTelephoneNo(String telephoneNo) {
		this.telephoneNo = telephoneNo;
	}

	public String getSalutation() {
		return salutation;
	}

	public void setSalutation(String salutation) {
		this.salutation = salutation;
	}
	
	public boolean addInterest(Interest interest) {
		return interests.add( interest );
	}
	
	public boolean removeInterest(Interest interest) {
		return interests.remove( interest );
	}
	
	public ArrayList<Interest> getInterests() {
		return interests;
	}
	
	public void setInterest(ArrayList<Interest> interests) {
		this.interests = interests;
	}
	
	public HashMap<String, Account> getAccountsMap() {
		return accounts;
	}

	public Account getAccount(String accountId) {
		return accounts.get(accountId);
	}
	
	public void putAccount(Account account) {
		accounts.put(account.getAccountNo(), account);
	}
	
	public ArrayList<Account> getAccounts() {
		ArrayList<Account> arrAccounts = new ArrayList<Account>(accounts.values());
		Collections.sort(arrAccounts);
		return arrAccounts;
	}
	
	// alert
	public Account getAccount() {
		//System.out.println( accounts.get( "20110001" ). );
		return accounts.get( "20110001" );
	}
	
	public String getCustomerStringForBill(){
		String customerStr = this.getSalutation();
		if(this.getFirstname() != null) customerStr += " " + this.getFirstname();
		if(this.getSecondname() != null) customerStr += " " + this.getSecondname();
		if(this.getAddress()!=null) customerStr = customerStr + "\n" + this.getAddress();
		return customerStr;
	}

	public String getCustomerAccountString(String accountno){
		String customerAccountStr = this.getSalutation();
		if(this.getFirstname() != null) customerAccountStr += " " + this.getFirstname();
		if(this.getSecondname() != null) customerAccountStr += " " + this.getSecondname();
		customerAccountStr = accountno + " : " + customerAccountStr;
        return (customerAccountStr);
	}

	
	public String toString() {
		String customerStr = this.getSalutation();
		if(this.getFirstname() != null) customerStr += " " + this.getFirstname();
		if(this.getSecondname() != null) customerStr += " " + this.getSecondname();
		return customerStr;
	}

	@Override
	public int compareTo(Customer arg0) {
		// TODO Auto-generated method stub
		return (getFullname().compareTo(arg0.getFullname()));
	}

}
